<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\admin\controller;
use app\models\Models;

/**
 * -----------------------------------------
 * Class Index
 * @package app\admin\controller
 * -----------------------------------------
 */
class Index extends Check{
    /**
     * @return \think\response\View
     * 后台首页
     */
    public function index(){
        $id = cookie('join_login');
        $admin = new Models('admin');
        $auth_rule = new Models('auth_rule');
        // 获取配置文件信息
        $config = self::getConfig();
        // 获取当前用户具备的权限
        $info = $admin->joinOne([['auth_group_access b','a.id = b.uid','left'],['auth_group c','b.group_id = c.id','left']],['a.id'=>$id],'a.username,c.title,c.rules');
        // 超级管理员至高无上,隐藏商家管理
        if($id == Models::GROUP_ADMIN){
            $parent_button = $auth_rule->_select("p_id = 0",'','orderby desc');
            $son_button    = $auth_rule->_select("p_id != 0 and is_show = 0 and orderby != 999",'','orderby desc');
        }else{
            $parent_button = $auth_rule->_select("id in ({$info['rules']}) and p_id = 0",'','orderby desc');
            $son_button    = $auth_rule->_select("id in ({$info['rules']}) and p_id != 0 and is_show = 0",'','orderby desc');
        }
        // 循环权限列表
        $button = [];
        foreach($parent_button as $k=>$v){
            foreach($son_button as $k1=>$v1){
                if($v1['p_id'] == $v['id']){
                    $button[$k]['parentButton'] = $v;
                    $button[$k]['sonButton'][]  = $v1;
                }
            }
        }
        return view('index',['config'=>$config,'button'=>$button,'info'=>$info]);
    }

    /**
     * @return \think\response\View
     * 后台主体页面
     */
    public function main(){
        $admin = new Models('admin');
        $user  = new Models('user');
        $order = new Models('order');
        $goods = new Models('goods');
        $activity = new Models('activity');
        $business = new Models('business');
        // 获取配置文件信息
        $config = self::getConfig();
        // 获取登录用户类型
        $group_arr = self::getGroup();
        // 获取登录信息
        $info = $admin->_find(['id'=>$group_arr['id']],'login_times,last_login_ip,last_login_time');
        // 时间处理
        $yestoday = strtotime(date('Y-m-d',strtotime('-1 day')));
        $today    = strtotime(date('Y-m-d'));
        $week     = week();
        $month    = month();
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = $group_arr['id'];

        } else {
            // 获取用户数量
            $u_list = $user->_select([],'id,create_time');
            // 获取商家数量
            $b_list = $business->joinMany([['admin b','a.id = b.id','left']],['a.id'=>['neq',Models::OFFICIAL]],'a.id,a.amount,b.create_time');
            // 获取订单数
            $o_list = $order->_select(['status'=>Models::ORDER_PAY_YES],'id,pay_amount,create_time');
            // 获取活动数
            $e_list = $activity->_select(['status'=>['neq',Models::STATUS_CANCEL]],'id,create_time');
            // 获取商品数
            $g_list = $goods->_select(['status'=>[['eq',Models::GOODS_STATUS_ON],['eq',Models::GOODS_STATUS_OFF],'or']],'id,create_time');
        }
        $info['user_count']     = count(array_column($u_list,'id'));    // 用户总数
        $info['business_count'] = count(array_column($b_list,'id'));    // 商家总数
        $info['amount_count']   = array_sum(array_column($o_list,'pay_amount'));    // 金额总数
        $info['order_count']    = count(array_column($o_list,'id'));    // 订单总数
        $info['event_count']    = count(array_column($e_list,'id'));    // 活动总数
        $info['goods_count']    = count(array_column($g_list,'id'));    // 商品总数

        // 获取用户今天，昨天，本周，本月数
        $u_day = 0;$u_yesday = 0;$u_week = 0;$u_month = 0;
        foreach($u_list as $v){
            if($v['create_time'] >= $today) $u_day++;
            if($v['create_time'] >= $yestoday && $v['create_time'] < $today) $u_yesday++;
            if($v['create_time'] >= $week['w_start'] && $v['create_time'] <= $week['w_end']) $u_week++;
            if($v['create_time'] >= $month['m_start'] && $v['create_time'] <= $month['m_end']) $u_month++;
        }
        $info['user_today'] = $u_day;
        $info['user_yestoday'] = $u_yesday;
        $info['user_week'] = $u_week;
        $info['user_month'] = $u_month;

        // 获取金额今天，昨天，本周，本月数
        $a_day = 0;$a_yesday = 0;$a_week = 0;$a_month = 0;
        foreach($o_list as $v){
            if($v['create_time'] >= $today) $a_day += $v['pay_amount'];
            if($v['create_time'] >= $yestoday && $v['create_time'] < $today) $a_yesday += $v['pay_amount'];
            if($v['create_time'] >= $week['w_start'] && $v['create_time'] <= $week['w_end']) $a_week += $v['pay_amount'];
            if($v['create_time'] >= $month['m_start'] && $v['create_time'] <= $month['m_end']) $a_month += $v['pay_amount'];
        }
        $info['amount_today'] = $a_day;
        $info['amount_yestoday'] = $a_yesday;
        $info['amount_week'] = $a_week;
        $info['amount_month'] = $a_month;
        return view('main',['config'=>$config,'info'=>$info]);
    }
}